import axios from 'axios';

const api = axios.create({
  baseURL: 'http://localhost:8080/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加 token
    const user = JSON.parse(localStorage.getItem('user') || '{}');
    if (user.id) {
      config.headers['Authorization'] = `Bearer ${user.id}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    if (error.response) {
      return Promise.reject(error.response.data);
    }
    return Promise.reject({ success: false, message: '网络错误' });
  }
);

export default api;

